//
//  SeriesCollectionViewController.m
//  Our Big Demo
//
//  Created by 张立强 on 16/3/25.
//  Copyright © 2016年 WJP. All rights reserved.
//

#import "ChannelCollectionViewController.h"
#import "ChannelCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "ChannelModel.h"
#import "GiveMeHleper.h"
@interface ChannelCollectionViewController ()<UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)NSMutableArray *allDataArray;
@end

@implementation ChannelCollectionViewController

static NSString * const reuseIdentifier = @"Channel";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handleData];
    
    
}
- (void)handleData{
    self.allDataArray = [NSMutableArray array];
    [[GiveMeHleper defaultCDHttpHelper]get:@"http://app.vmoiver.com/apiv3/cate/getList" params:nil success:^(id responseObj) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dict in dic[@"data"]) {
            ChannelModel *model = [ChannelModel new];
            [model setValuesForKeysWithDictionary:dict];
            [self.allDataArray addObject:model];
        }
        [self.collectionView reloadData];
        NSLog(@"解析数据成功");
    } failure:^(NSError *error) {
        NSLog(@"解析失败");
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.view.frame.size.width/2,  self.view.frame.size.width/2);
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.allDataArray.count;
}
//设置collection的item横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
//设置collection的item纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    ChannelModel *model = self.allDataArray[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"#%@#",model.catename];
    cell.ChineseLable.text = str;
    cell.EnglishLable.text = model.alias;
    [cell.ChannelImageView sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    

    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
